/**
 * Definition for a binary tree node.
 * type TreeNode struct {
 *     Val int
 *     Left *TreeNode
 *     Right *TreeNode
 * }
 */
func averageOfLevels(root *TreeNode) []float64 {
    ans := []float64{}
    if root == nil {
        return ans
    }
    res := [][]int{}
    tempArr := []int{}
    queue := list.New()
    queue.PushBack(root)

    for queue.Len() > 0 {
        length := queue.Len()
        for i:=0; i<length; i++ {
            node := queue.Remove(queue.Front()).(*TreeNode)
            tempArr = append(tempArr, node.Val)
            if node.Left != nil {
                queue.PushBack(node.Left)
            }
            if node.Right != nil {
                queue.PushBack(node.Right)
            }
        }
        res = append(res, tempArr)
        tempArr = []int{}
    }

    for i:=0; i<len(res); i++ {
        var sum int
        for j:=0; j<len(res[i]); j++ {
            sum += res[i][j]
        }
        av := float64(sum)/float64(len(res[i]))
        ans = append(ans, av)
    }
    
    return ans
}